package com.carhome.ui.base;

import com.lidroid.xutils.DbUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class BaseFragment extends Fragment {
	
	private DbUtils mDbUtils;
	private final static String DATABASE_CACHE = "datacache";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferences share  = getActivity().getSharedPreferences(DATABASE_CACHE, Context.MODE_PRIVATE);
		if (!share.getBoolean("firstopen", false)) {	//如果第一次进入程序
			SharedPreferences.Editor editor =  share.edit();
			editor.putBoolean("firstopen", true).commit();
			mDbUtils = DbUtils.create(getActivity(), DATABASE_CACHE);
		}else {
		}
		
	}

	/**
	 * 打开fragment
	 * 
	 * @param fm
	 *            传入的FragmentManager参数
	 * @param id
	 *            要装载的控件id
	 * @param classFragment
	 *            Fragment类名
	 */
	protected void openFragment(FragmentManager fm, int id, Class<?> classFragment) {
		openFragment(fm, id, classFragment, null);
	}

	/**
	 * 打开fragment 并设置tag
	 * 
	 * @param fm
	 *            传入的FragmentManager参数
	 * @param id
	 *            要装载的控件id
	 * @param classFragment
	 *            Fragment类名
	 * @param tag
	 *            为fragment设置tag
	 */
	protected void openFragment(FragmentManager fm, int id, Class<?> classFragment, String tag) {
		try {
			Fragment fragment = (Fragment) classFragment.newInstance();
			if (tag == null) {
				fm.beginTransaction().replace(id, fragment).commit();
			} else {
				fm.beginTransaction().replace(id, fragment, tag).commit();
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (java.lang.InstantiationException e) {
			e.printStackTrace();
		}
	}
}
